package erz.printing.police.settings;

import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceFragmentCompat;
import android.support.v7.preference.SeekBarPreference;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.Set;

import erz.printing.police.BaseActivity;
import erz.printing.police.R;


public class SettingsActivity extends BaseActivity {
    private final static String TAG = SettingsActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_settings);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        fixPlaceHolderHeight();

        getSupportFragmentManager().beginTransaction()
                .replace(R.id.content_view, new PrefsFragment()).commit();
    }

    private void fixPlaceHolderHeight() {
        int height = getActionBarHeight() + getStatusBarHeight();
        View view = findViewById(R.id.place_holder);
        ViewGroup.LayoutParams lp = view.getLayoutParams();
        lp.height = height;
        view.setLayoutParams(lp);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    public static class PrefsFragment extends PreferenceFragmentCompat {
        @Override
        public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
            // Load the preferences from an XML resource
            addPreferencesFromResource(R.xml.preferences);

            SeekBarPreference seekBar = (SeekBarPreference) findPreference("seek_bar_key");
            if (seekBar != null) {
                final SettingPreferences pref = new SettingPreferences(getActivity());
                seekBar.setValue(
                        pref.getInt(SettingPreferences.PHOTO_PRINT_DPI_KEY,
                            getActivity().getResources().getInteger(R.integer.photo_print_dpi_default)
                        )
                );
                seekBar.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
                    @Override
                    public boolean onPreferenceChange(Preference preference, Object newValue) {
                        if (newValue instanceof Integer) {
                            Integer newValueInt;
                            try {
                                newValueInt = (Integer) newValue;
                            } catch (NumberFormatException nfe) {
                                Log.e(TAG,
                                        "SeekBarPreference is a Integer, but it caused a NumberFormatException");
                                return false;
                            }
                            // Do something with the value
                            pref.put(SettingPreferences.PHOTO_PRINT_DPI_KEY, newValueInt);
                            return true;
                        } else {
                            String objType = newValue.getClass().getName();
                            Log.e(TAG, "SeekBarPreference is not a Integer, it is " + objType);
                            return false;
                        }
                    }
                });
            }
        }
    }
}
